/**
 *  SansEmotion
 *  Author: tom
 *  Description: 
 */

model SansEmotion
import "Common.gaml"
/* Insert your model definition here */
global{	
	
	float normal_speed <- 0.2;
	int nb_people <- 100;
	float probability_of_change <- 0.001;
	float max_human_size <- 2.0;
	int total_collision <- 0;
	init {
		create obstacle from : shape_file_obstacles returns: list_of_obstacle ;
		create background from : shape_file_bounds returns: list_of_bg ;
		create port from : shape_file_ports ;
		set obstacles <- obstacles union list ( list_of_obstacle ) ;
		set bg <- first ( list_of_bg ) ;
		ask target : bg {
			set shape <- self - obstacles ;
		}
		let ports type : list of : port <- list ( port ) ;		
		
		
		create people number : nb_people {
			set location <- any_location_in ( bg ) ;			
			//set the_target <- first ( ports ) ;
		}
	}
}


entities {
	species targ{
		rgb color <- rgb('red');
		aspect base {
			draw shape : circle size : 2 color : color ;
		}
	}
	
	species people skills: [moving, driving2d]{
		float size <- max_human_size;
		rgb color <- rgb('yellow');
		point tar <- nil;
		point prev <- nil;
		aspect base {
			draw shape : circle size : size color : color ;
		}
		/**
		 * quand les agents n'ont pas peur, ils déplacent lentement ou ne déplacent pas 
		 */
		reflex to_target when: tar != nil{

			//prev <- location;
			do move heading: self towards tar speed: normal_speed bounds: bg;
			
			//do wander speed: normal_speed bounds: bg;
		}
		reflex on_target when: tar != nil {
			if(self distance_to tar < size){				
				set tar <- nil;				
			}
		}
		
		reflex do_wander when: tar = nil{
			do wander speed: rnd(10)/10 bounds: bg;

			if(flip(probability_of_change)){
				let pos <- any_location_in(bg);
				set tar <- pos;
			}			
		}
		reflex evite_obstacle when: tar != nil {
			if(self overlaps prev){
				do move heading: heading + 89 speed: normal_speed bounds: bg;			
			}
		}
		
		reflex evite_obstacle2 when: tar != nil{
			let s <- 2*size;
			let obs type: list of: obstacle <-  (self neighbours_at s of_species obstacle);
						
			loop ob over: obs{
				let ds <- (self direction_to ob) - (heading);
				if(abs(ds) < 45.1){	
//write("change heading");									
//					do move heading: heading + 89 speed: normal_speed bounds: bg;		
				}
			}
		}
		reflex flee_people{
			//quand il y a des collisions, les vitesses des agents ralentissent
			//et peut-etre = 0
			//let closes type: list of: people <- people overlapping(self);
			let distance_to_flee <- 2 * max_human_size;	
			let closes type: list of: people <-  (self neighbours_at distance_to_flee of_species people);
			//set total_collision <- total_collision + length(closes);
			loop close over: closes{
				do move speed: speed/2 heading: self towards close + rnd(180)+ 45 bounds: bg;
			}
			
		}
		reflex collision{
			//let collision type: list of: people <- (self );
			let distance_collision <- 0.9 * max_human_size;
			let closes type: list of: people <-  (self neighbours_at distance_collision of_species people);
			set total_collision <- total_collision + length(closes);
		}
		
	}
}
environment bounds: shape_file_bounds ;
experiment sansEmo type: gui {
	output {
		display building_display refresh_every: 1 {
			species background aspect : base;
			species obstacle aspect: base ;
			species port aspect: base ;
			species people aspect: base ;
			species targ aspect: base ;							
		}
		monitor name:"Collision:" value: total_collision/2;
	}
	
}